/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import dto.Borrower;
import java.util.List;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

/**
 *
 * @author Scott Kellish
 */
public class BorrowerDAO {

    private SqlSessionFactory sqlSessionFactory;

    public BorrowerDAO() {
        sqlSessionFactory = MyBatisConnectionFactory.getSqlSessionFactory();
    }

    /**
     * Returns a list of all Borrower instances from the database
     *
     * @return the list of all Borrower instances from the database
     */
    public List<Borrower> getAll() {
        SqlSession session = sqlSessionFactory.openSession();
        try {
            List<Borrower> list = session.selectList("Borrower.getAll");
            return list;
        } finally {
            session.close();
        }
    }

    /**
     * Returns an Borrower instance from the database
     *
     * @param id primary key value used for the lookup
     * @return A Borrower instance with a primary key value equals to pk, or null
     * if there is no match
     */
    public Borrower getById(int id) {
        SqlSession session = sqlSessionFactory.openSession();
        try {
            Borrower borrower = (Borrower) session.selectOne("Borrower.getById", id);
            return borrower;
        } finally {
            session.close();
        }
    }
    
    /**
     * Returns a list of all Borrower instances from the database
     *
     * @return the list of all Borrower instances from the database
     */
    public List<Borrower> getChildrenOfTeacher(int teacher_id) {
        SqlSession session = sqlSessionFactory.openSession();
        try {
            List<Borrower> list = session.selectList("Borrower.getChildrenOfTeacher", teacher_id);
            return list;
        } finally {
            session.close();
        }
    }
    
        /**
     * Returns a list of all Borrower instances from the database
     *
     * @return the list of all Borrower instances from the database
     */
    public List<Borrower> getTeachers() {
        SqlSession session = sqlSessionFactory.openSession();
        try {
            List<Borrower> list = session.selectList("Borrower.getTeachers");
            return list;
        } finally {
            session.close();
        }
    }

    
    /**
     * * Updates an instance of Item in the database.
     *
     * @param item the instance to be updated.
     */
    public void update(Borrower borrower) {
        SqlSession session = sqlSessionFactory.openSession();
        try {
            session.update("Borrower.update", borrower);
            session.commit();
        } finally {
            session.close();
        }
    }

    /**
     * * Insert an instance of Borrower into the database.
     *
     * @param borrower the instance to be persisted.
     */
    public void insert(Borrower borrower) {
        SqlSession session = sqlSessionFactory.openSession();
        try {
            session.insert("Borrower.insert", borrower);
            session.commit();
        } finally {
            session.close();
        }
    }

    /**
     * * Delete an instance of Borrower from the database.
     *
     * @param id primary key value of the instance to be deleted.
     */
    public void delete(int id) {
        SqlSession session = sqlSessionFactory.openSession();
        try {
            session.delete("Borrower.deleteById", id);
            session.commit();
        } finally {
            session.close();
        }
    }
    
    /**
     * Truncates the item table and resets the next rowid() back to 1
     */
    public void truncate() {
        SqlSession session = sqlSessionFactory.openSession();
        try {
            session.delete("Borrower.truncateDelete");
            session.delete("Borrower.truncateResetRowId");
            session.commit();
        } finally {
            session.close();
        }        
    }
    
}
